package com.gxuwz.medical.domain.payRecord;

import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;

/**
 * 缴费记录
 * @author Administrator
 *
 */
public class PayRecord {

	private String chzNumber;    //参合发票号
	private String chzcode;      //参合证号
	private String name;      	 //姓名
	private String familycode;	 //家庭编号
	private String housemaster;	 //户主
	private String housecode; 	 //户内编号
	private String payYear;   	 //缴费年度 
	private double payAmount; 	 //缴费金额 
	private Date payTime;   	 //缴费时间
	private String register;  	 //操作员
	
	public PayRecord(){
		super();
		}

	public PayRecord(String housecode, String payYear) throws SQLException {
		this.housecode = housecode;
		this.payYear = payYear;
		getPayRecord();
	}
	
	public PayRecord(String chzNumber, String chzcode, String name,
			String familycode, String housemaster, String housecode,
			String payYear, double payAmount, Date payTime, String register) {
		super();
		this.chzNumber = chzNumber;
		this.chzcode = chzcode;
		this.name = name;
		this.familycode = familycode;
		this.housemaster = housemaster;
		this.housecode = housecode;
		this.payYear = payYear;
		this.payAmount = payAmount;
		this.payTime = payTime;
		this.register = register;
	}

	@Override
	public String toString() {
		return "PayRecord [familycode=" + familycode + ", housemaster=" + housemaster + ", housecode=" + housecode
				+ ", chzcode=" + chzcode + ", chzNumber=" + chzNumber + ", name=" + name + ", payAmount="
				+ payAmount + ", payYear=" + payYear + ", payTime=" + payTime + ", register=" + register + "]";
	}
	public String getChzNumber() {
		return chzNumber;
	}

	public void setChzNumber(String chzNumber) {
		this.chzNumber = chzNumber;
	}

	public String getChzcode() {
		return chzcode;
	}

	public void setChzcode(String chzcode) {
		this.chzcode = chzcode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFamilycode() {
		return familycode;
	}

	public void setFamilycode(String familycode) {
		this.familycode = familycode;
	}

	public String getHousemaster() {
		return housemaster;
	}

	public void setHousemaster(String housemaster) {
		this.housemaster = housemaster;
	}

	public String getHousecode() {
		return housecode;
	}

	public void setHousecode(String housecode) {
		this.housecode = housecode;
	}

	public String getPayYear() {
		return payYear;
	}

	public void setPayYear(String payYear) {
		this.payYear = payYear;
	}

	public double getPayAmount() {
		return payAmount;
	}

	public void setPayAmount(double payAmount) {
		this.payAmount = payAmount;
	}

	public Date getPayTime() {
		return payTime;
	}

	public void setPayTime(Date payTime) {
		this.payTime = payTime;
	}

	public String getRegister() {
		return register;
	}

	public void setRegister(String register) {
		this.register = register;
	}
	
	public String getPayRecord() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String housecode = null;
		try{
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_payRecord where  housecode=? and payYear=? ");
			int index = 1;
			pstmt.setString(index++, this.housecode);
			pstmt.setString(index++, this.payYear);
			// 执行查询操作
			rs = pstmt.executeQuery();
			if(rs.next()) {
				housecode = rs.getString("housecode");
			}
			return housecode;
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
	
	public void AddPayRecord()throws Exception{
		Connection conn = null;
		try{
			conn = DbUtil.getConn();
			// 2:开启手动提交事务
			conn.setAutoCommit(false);
			// 3:保存家庭档案信息到数据库
			addPayRecord(conn);
			// 4:提交事务
			conn.commit();
		}catch (Exception e) {
			// 事务回滚
			if(conn!=null){
				conn.rollback();
			}
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	

	private void addPayRecord(Connection conn)throws SQLException{
		  PreparedStatement pstmt = null;
		  try{
			  // 创建SQL语句
			  StringBuffer sqlBuff = new StringBuffer("insert into t_payRecord(familycode, housemaster, housecode, "
			  		+ "chzcode, chzNumber, name, payAmount, payYear, payTime, register)");
			  sqlBuff.append(" values(?,?,?,?,?,?,?,?,?,?)");
			  pstmt = conn.prepareStatement(sqlBuff.toString());
			  // 把当前对象的值赋给占位符
			  int index = 1;
			  pstmt.setString(index++, this.familycode);
			  pstmt.setString(index++, this.housemaster);
			  pstmt.setString(index++, this.housecode);
			  pstmt.setString(index++, this.chzcode);
			  pstmt.setString(index++, this.chzNumber);
			  pstmt.setString(index++, this.name);
			  pstmt.setDouble(index++, this.payAmount);
			  pstmt.setString(index++, this.payYear);
			  pstmt.setDate(index++, new java.sql.Date(this.payTime.getTime()));
			  pstmt.setString(index++, this.register);
			  // 执行添加操作
			  pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
	}
	
	public boolean DeletePayRecord()throws SQLException{
		boolean flag = false;
		Connection conn = null;
		try{
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			deletePayRecord(conn);
			flag = true;
			conn.commit();
		}catch (SQLException e) {
			if(conn!=null){
				// 事务回滚
				conn.rollback();
			}
			throw e;
		}finally{
			DbUtil.close(conn);
		}
		return flag;
	}
	private void deletePayRecord(Connection conn) throws SQLException {
		PreparedStatement pstmt=null;
		try{
			// 创建SQL语句
			StringBuffer sqlBuff = new StringBuffer("delete from t_payRecord where familycode=? ");
			pstmt=conn.prepareStatement(sqlBuff.toString());
			// 把当前对象的值赋给占位符
			int index = 1;
			pstmt.setString(index++, this.familycode);
			// 执行删除操作
			pstmt.executeUpdate(); 
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
		} 
	}
	
	
	
}
